﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace theguineasyndrome
{
    public class Cloud : ILevelObject
    {
        private static Random random = new Random();
        private Level level;
        private Texture2D texture;
        private Vector2 position;
        private Vector2 origin;
        private float rotation;
        private float scale;
        private Color color;
        private float radius;
        private float rotationSpeed;

        private bool dead;

        public bool Dead
        {
            get { return dead; }
        }

        public Cloud(Level level, Texture2D texture, Vector2 position)
        {
            this.level = level;
            this.texture = texture;
            this.position = position;
            origin = new Vector2(texture.Width, texture.Height) / 2;
            rotation = 0;
            scale = 1;
            color = Color.White;
            dead = false;
            radius = (texture.Width + texture.Height) / 2 / 2;
            rotationSpeed = (float)(random.NextDouble() - 0.5) * 2;
        }

        public void Update(GameTime gameTime)
        {
            double t = gameTime.ElapsedGameTime.TotalMilliseconds;
            rotation += (float)(t / 250 * rotationSpeed);
            scale -= (float)(t / 1000);
            color.A = (byte)(scale * 255);
            color.R = color.A;
            color.G = color.A;
            color.B = color.A;
            dead = scale <= 0;
        }

        public void Draw(SpriteBatch spriteBatch, Camera camera)
        {
            spriteBatch.Draw(texture, camera.getDstPoint(position), null, color, rotation, origin, scale, SpriteEffects.None, 0);
        }

        public float Depth
        {
            get { return position.Y + scale * radius; }
        }

        public void queueDrawing(Camera camera)
        {
            Rectangle rectangle = new Rectangle((int)(position.X - radius * scale), (int)(position.Y - radius * scale), (int)(radius * scale * 2), (int)(radius * scale * 2));

            if (camera.isVisible(rectangle))
                level.queueObjectDrawing(this);
        }
    }
}
